<template>
  <div class="deviceManage">
    <!-- <c-title :hide="false" text="设备管理"></c-title> -->
    <div class="desc">
      <backBtn :icon="true"></backBtn>
      当前设备管理
    </div>
    <div class="deviceList">
      <div class="lis" v-for="(item, ind) in listData" :key="ind" @click="toDeatail(item)">
        <div class="top">
          <div class="top_l">{{ item.machine_type_name }}</div>
          <div class="top_r">
            <!-- <van-icon name="arrow-up" color="#AAAAB3" v-if="item.open" />
            <van-icon name="arrow-down" color="#AAAAB3" v-else /> -->
          </div>
        </div>
        <div class="device">
          <div class="device_l">
            <div class="txt device_l_t_b">设备名称：{{ item.name }}</div>
            <div class="txt device_l_t_b">设备编号：{{ item.local_code }}</div>
          </div>
          <div class="device_r" @click.stop="cancelBtn(item.id)">
            <div class="btn">解绑</div>
          </div>
        </div>
        <template v-for="kitem in item.part_no_list">
          <div class="parts" :key="kitem.part_no" v-if="kitem.id != 0">
            <div class="parts_l">配件编码：{{ kitem.code }}</div>
            <div class="parts_r">
              <div class="txt">{{ kitem.name }}</div>
              <div class="txt parts_r_t" @click.stop="cancelCode(kitem.id)">解绑</div>
            </div>
          </div>
          <template v-else>
            <div class="parts" :key="kitem.part_no">
              <div class="parts_l">配件编码：暂无配件</div>
              <div class="parts_r">
                <div class="txt">{{ kitem.name }}</div>
                <div class="txt parts_r_t" @click.stop="toBindCode(item, index, kitem)">绑定</div>
              </div>
            </div>
          </template>
        </template>
        <div class="device">
          <div class="device_l" style="justify-content: center;">
            <div class="txt device_l_t_b" style="margin-bottom: 0;">到期时间：{{ item.end_time }}</div>
          </div>
          <div class="device_r">
            <!-- <div class="btn" v-if="!item.has_one_pay" @click.stop="tapBtnMethod('1', item.id)">签约</div> -->
            <div class="btn" v-if="item.has_one_pay && item.has_one_pay.type == 1" @click.stop="tapBtnMethod('1', item.id)">续约</div>
            <div class="btn" v-if="item.has_one_pay && item.has_one_pay.type != 1" @click.stop="tapBtnMethod('2', item.id)">解约</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "../../utils/mixin"; //引入加载更多
import backBtn from "./components/backBtn.vue";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      listData: [],

      page: 1,
      total_page: 0,
      isLoadMore: true
    };
  },
  activated() {
    this.init();
    this.getData();
  },
  components: { backBtn },
  methods: {
    init() {
      this.page = 1;
      this.total_page = 1;
      this.isLoadMore = true;
      this.listData = [];
    },
    tapBtnMethod(key, id) {
      let urlstr = "";
      // if (key == 1) { 签约废弃客户要求
      //   this.$router.push(this.fun.getUrl("alipayCloud", { status: "drink" }, { url: "plugin.drink-machine.frontend.machine.machineAgree", machine_id: id }));
      //   return;
      // } else 
      if (key == 1) {
        // 续费
        urlstr = "plugin.drink-machine.frontend.machine.extraPayUrl";
        $http.post(urlstr, { machine_id: id }, "加载中...").then(res => {
          if (res.result == 1) {
            window.location.href = res.data;
          } else {
            this.$toast(res.msg);
          }
        });
        return;
      } else {
        // 解约
        urlstr = "plugin.drink-machine.frontend.machine.agreeCancel";
      }
      $http.post(urlstr, { machine_id: id }, "加载中...").then(res => {
        if (res.result == 1) {
          this.$toast("解约成功");
          this.init();
          this.getData();
        } else {
          this.$toast(res.msg);
        }
      });
    },
    toDeatail(item) {
      console.log(item);
      this.$router.push(this.fun.getUrl("drinkMachindIndex", {}, { machine_type: item.machine_type, id: item.id }));
    },
    getData() {
      $http.post("plugin.drink-machine.frontend.machine.machineList", { machine_type: this.$route.params.id, page: this.page }, "加载中...").then(res => {
        if (res.result == 1) {
          console.log(res.data);
          this.isLoadMore = true;
          this.listData = res.data.data;
          this.total_page = res.data.last_page;
          if (!this.total_page) {
            this.total_page = 0;
          }
        } else {
          this.$toast(res.msg);
        }
      });
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        console.log("more");
        $http.post("plugin.drink-machine.frontend.machine.machineList", { machine_type: this.$route.params.id, page: this.page }, "加载中...").then(res => {
          if (res.result == 1) {
            let list = res.data.data;
            this.total_page = res.data.last_page;
            this.listData = [...this.listData, ...list];
            if (!this.total_page) {
              this.total_page = 0;
            }
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$toast(res.msg);
          }
        });
      }
    },
    // 解绑
    cancelBtn(sid) {
      $http.post("plugin.drink-machine.frontend.machine.cancel", { id: sid }, "加载中...").then(res => {
        if (res.result == 1) {
          this.getData();
          this.$toast(res.msg);
        } else {
          this.$toast(res.msg);
        }
      });
    },
    cancelCode(id) {
      $http.post("plugin.drink-machine.frontend.part.cancel", { part_id: id }, "加载中...").then(res => {
        if (res.result == 1) {
          this.getData();
          this.$toast(res.msg);
        } else {
          this.$toast(res.msg);
        }
      });
    },
    toBindCode(item, index, kitem) {
      this.$router.push(this.fun.getUrl("bindPj", { pj: 1 }, { machine_id: item.id, part_no: kitem.part_no, noTitle: kitem.name }));
    }
  }
};
</script>
<style lang="scss" scoped>
.deviceManage {
  padding-bottom: 2rem;
}

.desc {
  margin: 0.84rem 0.75rem;
  font-size: 0.94rem;
  font-weight: 500;
  text-align: left;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.deviceList {
  margin: 0.75rem;

  .lis {
    padding: 0.94rem 0.75rem;
    border-radius: 0.75rem;
    background: #fff;
    margin-bottom: 0.5rem;

    .parts {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 0.75rem;
      margin-bottom: 0.63rem;

      .parts_l {
        flex: 1;
        text-align: left;
      }

      .parts_r {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        .parts_r_t {
          font-size: 0.75rem;
          font-weight: 500;
          color: #015ef5;
          margin-left: 0.47rem;
        }
      }
    }

    .parts:last-child {
      margin-bottom: 0;
    }

    .device {
      display: flex;
      justify-content: space-between;

      .device_l {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        font-size: 0.75rem;
        font-weight: 400;
        color: #3b3b4a;
        line-height: 0.85rem;

        .device_l_t_b {
          margin-bottom: 0.63rem;
        }
      }

      .device_r {
        flex-shrink: 0;

        .btn_o {
          width: 3.31rem;
          height: 1.75rem;
          line-height: 1.75rem;
          text-align: center;
          font-size: 0.81rem;
          font-weight: 400;
          color: #015ef5;
          border: 0.03rem solid #015ef5;
          box-sizing: border-box;
          border-radius: 0.94rem;
        }
      }
    }

    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.53rem;

      .top_l {
        font-weight: 500;
        color: #00001c;
        font-size: 1rem;
      }

      .top_r {
        flex-shrink: 0;
      }
    }
  }
}

.btn {
  min-width: 3.31rem;
  height: 1.75rem;
  line-height: 1.75rem;
  text-align: center;
  font-size: 0.81rem;
  font-weight: 400;
  background: #015ef5;
  color: #fff;
  border: 0.03rem solid #015ef5;
  box-sizing: border-box;
  border-radius: 0.94rem;
}

.plus-icon-enter-active {
  transition: opacity 0.5s;
}

.plus-icon-enter {
  opacity: 0;
}

.plus-icon-leave-active {
  transition: opacity 0.1s;
}

.plus-icon-leave-to {
  opacity: 0;
}
</style>
